package com.huwei.modules.base.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringUtil {

	/**
	 * 删除指定前缀
	 * @param str
	 * @param prefix
	 * @return
	 */
	static public String delPrefix(String str ,String prefix) {
		if(null == str || null == prefix|| str.equals("")) {return str;}
		String result = str;
		int strLength = str.length(); 
		int prefixLength = prefix.length();
		for(int i = 0 ;i < strLength ;i += prefixLength) {
			int index = -1;
			if((index = result.indexOf(prefix)) == 0) {
				result = result.substring(index + prefixLength);
			}
			else {
				break;
			}
		}
		return result;
	}
	
	/**
	 * 删除指定后缀
	 * @param str
	 * @param suffix
	 * @return
	 */
	static public String delSuffix(String str ,String suffix) {
		if(null == str || null == suffix || str.equals("")) {return str;}
		String result = str;
		int strLength = str.length();
		int suffixLength = suffix.length();
		for(int i = strLength -1 ;i >= 0 ;i -= suffixLength) {
			int index = -1;
			if((index = result.lastIndexOf(suffix)) == result.length() - suffixLength) {
				result = result.substring(0 ,index);
			}
			else {
				break;
			}
		}
		return result;
	}
	
	/**
	 * 添加前缀
	 * @param str 原始字符
	 * @param prefix 前缀
	 * @param allLength 添加后总体长度，添加后的字符总体长度总是小于等于allLength ，出现这样的原因是：prefix 与str不能取整；
	 * @return
	 */
	static public String addPrefix(String str ,String prefix ,int allLength) {
		StringBuffer sb = new StringBuffer();
		while(sb.length() + prefix.length() + str.length() <= allLength) {
			sb.append(prefix);
		}
		sb.append(str);
		return sb.toString();
	}
	/**
	 * 
	 * @param str 原始字符
	 * @param suffix 后缀
	 * @param allLength 添加后总体长度，添加后的字符总体长度总是小于等于allLength ，出现这样的原因是：suffix 与str不能取整；
	 * @return
	 */
	static public String addSuffix(String str ,String suffix ,int allLength) {
		StringBuffer sb = new StringBuffer(str);
		while(sb.length() + suffix.length() <= allLength) {
			sb.append(suffix);
		}
		return sb.toString();
	}
	
	
	/**
	 * 	将字符串依据分割字符转换为List
	 * @param str
	 * @param regex
	 * @return
	 */
	static public List<String> switchListBySplitRegex(String str ,String regex) {
		if(str == null || regex == null) {return new ArrayList<String>();}
		return Arrays.asList(str.split(regex));
	}
	
	/**
	 * 	从多个分割字符中选出一种，将指定的字符数据分割并转换为List
	 * @param str
	 * @param regex
	 * @return
	 */
	static public List<String> switchListBySplitRegexs(String str ,String ...regex){
		if(str == null || regex == null || regex.length == 0) {return new ArrayList<String>();}
		List<String> res = new ArrayList<String>();
		for(int i = 0 ,len = regex.length ;i < len ;++i) {
			res = switchListBySplitRegex(str, regex[i]);
			if(res != null ) {return res;}
		}
		return res;
	}
	
	/**
	 *	 失败返回空
	 * @param unique
	 * @return
	 */
	static public Long stringToLong(String unique) {
		Long id = null;
		try {
			id = Long.parseLong(unique);
		} catch (Exception e) {
		}
		return id;
	}
	/**
	 * 	失败 返回空值
	 * @param unique
	 * @return
	 */
	static public Integer stringToInteger(String unique) {
		Integer id = null;
		try {
			id = Integer.parseInt(unique);
		} catch (Exception e) {
		}
		return id;
	}
	
	/**
	 * 	失败 返回空值
	 * @param unique
	 * @return
	 */
	static public Short stringToShort(String unique) {
		Short id = null;
		try {
			id = Short.parseShort(unique);
		} catch (Exception e) {
		}
		return id;
	}
}
